package at.fhj.pse.pageobjects;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


public class IntervalsPerMonthPage{
    private WebDriver driver;
    
    /*
     * Constructor
     */
    public IntervalsPerMonthPage(WebDriver driver){
        this.driver = driver;
    }
    

    /*
     * Input field: username:String
     */
    private String username;
    public String getUsername(){
        return username;
    }
    public void setUsername(String username){
        this.username = username;
    }
    
    
    /*
     * Input field: password:String
     */
    private String password;
    public String getPassword(){
        return password;
    }
    public void setPassword(String password){
        this.password = password;
    }
    
    /*
     * Input field: acceptNextAlert:Boolean
     */
    private Boolean acceptNextAlert;
    public Boolean getacceptNextAlert(){
    	return acceptNextAlert;
    }
    public void setacceptNextAlert(Boolean acceptNextAlert){
    	this.acceptNextAlert = acceptNextAlert;
    }
    
    /*
     * Action: showIntervalsperMonth
     */
    
    public String showIntervalsPerMonth () throws InterruptedException{
    	
    	driver.findElement(By.id("j_idt35:month_label")).click();
    	driver.findElement(By.cssSelector("li.ui-selectonemenu-item:nth-child(12)")).click();
    	
    	driver.findElement(By.id("j_idt35:year_label")).click();
    	driver.findElement(By.xpath("//div[@id='j_idt35:year_panel']/div/ul/li[3]")).click();
		driver.findElement(By.id("j_idt35:j_idt42")).click();
    	
    	String smsg = getSelectorMessage("25/11/2014", "tr.ui-widget-content.ui-datatable-even > td");
		assertEquals("25/11/2014", smsg);

		String xmsg = getPathMessage("10h 30min", "//tbody[@id='j_idt35:outMonthList_data']/tr/td[2]");
		assertEquals("10h 30min", xmsg);

		String xmsg2 = getPathMessage("2h 0min", "//tbody[@id='j_idt35:outMonthList_data']/tr/td[3]");
		assertEquals("2h 0min", xmsg2);

		String xmsg3 = getPathMessage("+ 8h 30min", "//tbody[@id='j_idt35:outMonthList_data']/tr/td[4]");
		assertEquals("+ 8h 30min", xmsg3);

		String xmsg4 = getPathMessage("28h 30min", "//tfoot[@id='j_idt35:outMonthList_foot']/tr/td[2]");
		assertEquals("28h 30min", xmsg4);

		String xmsg5 = getPathMessage("6h 0min", "//tfoot[@id='j_idt35:outMonthList_foot']/tr/td[3]");
		assertEquals("6h 0min", xmsg5);

		
		return getPathMessage("+ 22h 30min", "//tfoot[@id='j_idt35:outMonthList_foot']/tr/td[4]");
    }
    
    /*
     * Action: SelectorMessage
     */
    public String getSelectorMessage(String expected, String selector) throws InterruptedException{
    	String cssSelector = driver.findElement(By.cssSelector(selector)).getText();
    	for (int second = 0;; second++) {
        	if (second >= 60) fail("timeout");
        	try { if (expected.equals(cssSelector)) break; } catch (Exception e) {}
        	Thread.sleep(1000);
        }
    	return cssSelector;
    }
    
    /*
     * Action: PathMessage
     */
    public String getPathMessage(String expected, String Path) throws InterruptedException{
    	String xPath = driver.findElement(By.xpath(Path)).getText();
    	for (int second = 0;; second++) {
        	if (second >= 60) fail("timeout");
        	try { if (expected.equals(driver.findElement(By.xpath(Path)).getText())) break; } catch (Exception e) {}
        	Thread.sleep(1000);
        }
    	return xPath;
    }
    


   
}
